"""封装面包屑的方法"""


def get_breadcrumb(category):
    """
    获取面包屑导航的方法
    :param category: 商品类别
    :return: 面包屑字典 一级：返回一级；二级：返回一级+二级；三级：返回一级+二级+三级
    """
    # 创建面包屑字典
    breadcrumb = {
        'cat1': '',
        'cat2': '',
        'cat3': '',
    }

    if category.parent == None:  # 说明是一级类别
        breadcrumb['cat1'] = category
    elif category.subs.count() == 0: # 说明是三级类别
        cat2 = category.parent
        breadcrumb['cat2'] = cat2
        breadcrumb['cat3'] = category
        breadcrumb['cat1'] = cat2.parent
    else:  # 说明是二级类别
        breadcrumb['cat2'] = category
        breadcrumb['cat1'] = category.parent

    return breadcrumb
